﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Code4WP7SDK
{
    public class WeakAction
    {
        private readonly Action _action;
        private WeakReference _reference;

        public WeakAction(object target, Action action)
        {
            _reference = new WeakReference(target);
            _action = action;
        }

        public Action Action
        {
            get
            {
                return _action;
            }
        }

        public bool IsAlive
        {
            get
            {
                return _reference == null ? false : _reference.IsAlive;
            }
        }

        public object Target
        {
            get
            {
                return _reference == null ? null : _reference.Target;
            }
        }

        public void Execute()
        {
            if (_action != null && IsAlive)
            {
                _action();
            }
        }

        public void DeleteReference()
        {
            _reference = null;
        }
    }
}
